package com.bingo.mpa.dao;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import com.bingo.mpa.pojo.Agent;

public class AgentDao
{
	public static Agent findByName(String name)
	{
		final Connection conn = ConnectionPool.getConnection();
        if(conn == null )
        {
            return null;
        }
        PreparedStatement stmt = null;
        ResultSet rs = null;
        try
        {
        	conn.setAutoCommit(false);
            String sql = "SELECT * FROM P_AGENT WHERE NAME LIKE ?";
            stmt = conn.prepareStatement(sql);
            stmt.setString(1,name);
            rs = stmt.executeQuery();

            if(rs.next())
            {
                Agent obj = new Agent();
                obj.setAid(rs.getLong("AID"));
                obj.setName(rs.getString("NAME"));
                obj.setFullname(rs.getString("FULLNAME"));
                return obj;
            }
        }
        catch(Exception e)
        {
            e.printStackTrace();
        }
        finally
        {
            try
            {
                if( rs != null )
                    rs.close();
                if( stmt != null )
                    stmt.close();
                if(conn != null)
                {
                	conn.close();
                }
            }
            catch (SQLException ex)
            {
            }
        }
        return null;
	}
}
